package org.annoflow.audit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.annoflow.policy.PolicyManager;
import org.annoflow.policy.PolicyType;

public abstract class Logger {
	public enum LogLevel {
		ERROR, OUT
	}
	
	private static Map<String,List<Data>> filterToData = new HashMap<String,List<Data>>();
	
	/**
	 * Report an error audit to annoflow
	 * @param elements a stack trace at the {@link PolicyType#audit()} call.
	 * @param object the object being audited
	 * @param auditText the text created by the audit.
	 */
	public synchronized static void error(String filterName, Class<?> classOwner, Object object, String auditText)
	{
		addToLog(LogLevel.ERROR,filterName,classOwner, object, auditText);
	}

	/**
	 * Report an event (Non-error) audit to annoflow
	 * @param elements a stack trace at the {@link PolicyType#audit()} call.
	 * @param object the object being audited
	 * @param auditText the text created by the audit.
	 */
	public synchronized static void out(String filterName, Class<?> classOwner, Object object, String auditText)
	{
		addToLog(LogLevel.OUT,filterName,classOwner, object, auditText);
	}
	
	private static void addToLog(LogLevel level, String filterName, Class<?> classOwner, Object object, String auditText) {
		PolicyType objectPolicy = PolicyManager.lookupObjectPolicy(object);
		Data toLog = new Data(level,classOwner,objectPolicy,auditText);
		List<Data> filterData = filterToData.get(filterName);
		if(filterData == null)
		{
			filterData = new LinkedList<Data>();
			filterToData.put(filterName, filterData);
		}
		filterData.add(toLog);
	}
	public synchronized static Set<String> getFilterPoints()
	{
		return filterToData.keySet();
	}
	public synchronized static List<Data> getFilterData(String filterPoint)
	{
		return filterToData.get(filterPoint);
	}
	
}
